package com.demo.expandablistview;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myalldemo.R;

public class MainExpandableListViewActivity extends Activity {

	private ExpandableListView expandableListView;
	private List<String> group_list;
	private List<List<String>> item_list;
	private List<List<Integer>> item_list2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.expandablelistview_main);

		group_list = new ArrayList<String>();
		group_list.add("111");
		group_list.add("222");
		group_list.add("333");

		item_list = new ArrayList<List<String>>();
		item_list.add(group_list);
		item_list.add(group_list);
		item_list.add(group_list);

		List<Integer> temp_list = new ArrayList<Integer>();
		temp_list.add(R.drawable.ic_launcher);
		temp_list.add(R.drawable.ic_launcher);
		temp_list.add(R.drawable.ic_launcher);

		item_list2 = new ArrayList<List<Integer>>();
		item_list2.add(temp_list);
		item_list2.add(temp_list);
		item_list2.add(temp_list);

		expandableListView = (ExpandableListView) findViewById(R.id.expandablelist);
		expandableListView.setAdapter(new MyExpandableListViewAdapter(this));
	}

	public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
		private Context c;

		public MyExpandableListViewAdapter(Context context) {
			this.c = context;
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return item_list.get(groupPosition).get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			ItemHolder ih;
			if (convertView == null) {
				ih = new ItemHolder();
				convertView = getLayoutInflater().from(c).inflate(
						R.layout.item_expendlist_content, null);
				ih.img = (ImageView) convertView.findViewById(R.id.img);
				ih.txt = (TextView) convertView.findViewById(R.id.txt);
				convertView.setTag(ih);
			} else {
				ih = (ItemHolder) convertView.getTag();
			}
			ih.txt.setText(item_list.get(groupPosition).get(childPosition));
			ih.img.setBackgroundResource(item_list2.get(groupPosition).get(childPosition));
			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return item_list.get(groupPosition).size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return group_list.get(groupPosition);
		}

		@Override
		public int getGroupCount() {
			return group_list.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			GroupHolder gh;
			if (convertView == null) {
				gh = new GroupHolder();
				convertView = getLayoutInflater().from(c).inflate(
						R.layout.item_expendlist_head, null);
				gh.img = (ImageView) convertView.findViewById(R.id.head_img);
				gh.txt = (TextView) convertView.findViewById(R.id.head_txt);
				convertView.setTag(gh);
			} else {
				gh = (GroupHolder) convertView.getTag();
			}
			gh.txt.setText(group_list.get(groupPosition));
			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		public class GroupHolder {
			TextView txt;
			ImageView img;
		}

		public class ItemHolder {
			TextView txt;
			ImageView img;
		}

	}

}
